<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="primary" @click="showDialogAddCate">添加分类</el-button>
      <tree-table
        ref="treeTableRef"
        index-text="#"
        :data="treeTableData"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        stripe
        border
        show-index
        class="tree-table"
      >
        <template slot="isOk" scope="scope">
          <i
            class="el-icon-success"
            v-if="!scope.row.cat_deleted"
            style="color:lightgreen;font-size:24px"
          ></i>
          <i class="el-icon-error" v-else style="color:red;font-size:24px"></i>
        </template>
        <template slot="order" scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level == 1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level == 2" type="warning">三级</el-tag>
        </template>
        <template slot="operation">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog
      title="添加分类"
      :visible.sync="dialogAddCateVisible"
      width="50%"
      @close="closeDialogAddCate"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称：
" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            expand-trigger="hover"
            v-model="selectIdArr"
            :options="options"
            :props="cascaderProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAddCateVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  mounted() {
    this.getTreetableData()
  },
  data() {
    return {
      queryInfo: {
        type: '',
        pagenum: 1,
        pagesize: 5
      },
      treeTableData: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'operation'
        }
      ],
      dialogAddCateVisible: false,
      addCateForm: {
        addCateForm: '',
        cat_pid: 0,
        cat_level: 0
      },
      selectIdArr: [],
      options: [],
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      cascaderProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  methods: {
    closeDialogAddCate() {
      this.$refs.addCateFormRef.resetFields()
      this.selectIdArr = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    addCate() {
      this.$refs.addCateFormRef.validate(async valid => {
        if (!valid) return
        if (this.selectIdArr.length > 0) {
          let xxx = this.selectIdArr[this.selectIdArr.length - 1]
          this.addCateForm.cat_level = this.selectIdArr.length
          this.addCateForm.cat_pid = xxx
        }
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (res.meta.status !== 201) {
          this.$message.error(res.meta.msg)
          return
        }
        this.$message.success(res.meta.msg)
        this.dialogAddCateVisible = false
      })
    },
    async getTreetableData() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.treeTableData = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getTreetableData()
    },
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getTreetableData()
    },
    async showDialogAddCate() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.options = res.data
      this.dialogAddCateVisible = true
    },
    handleChange() {
      console.log(this.selectIdArr, 'selectIdArr')
    }
  }
}
</script>
<style lang="less" scoped>
.tree-table {
  margin: 20px 0;
}
.el-cascader {
  width: 100%;
}
</style>
